package com.wuxiaoqin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wuxiaoqin.server.entity.Commodity;
import com.wuxiaoqin.server.entity.Shoppings;
import com.wuxiaoqin.server.mapper.CommodityMapper;
import com.wuxiaoqin.server.mapper.ShoppingsMapper;
import com.wuxiaoqin.server.service.IShoppingsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuxiaoqin.server.vo.CartResult;
import com.wuxiaoqin.server.vo.CartVo;
import com.wuxiaoqin.uitls.respObj.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-11-03
 */
@Service
public class ShoppingsServiceImpl extends ServiceImpl<ShoppingsMapper, Shoppings> implements IShoppingsService {

    @Autowired(required = false)
    private ShoppingsMapper shoppingsMapper;
    @Autowired
    private CommodityMapper commodityMapper;


    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    /**
     * 加入购物车
     * @param
     * @param uid
     * @param cid
     * @param size
     * @param request
     * @return
     */
    @Override
    public RespBean addShopCart(Integer uid, Integer cid, String size, HttpServletRequest request) {

        Shoppings ucart = shoppingsMapper.selectOne(new QueryWrapper<Shoppings>().eq("C_id", cid).eq("U_id", uid).eq("S_size", size));
        if (ucart == null) {
            Shoppings cart = new Shoppings();
            cart.setUId(uid);
            cart.setCId(cid);
            cart.setSSize(size);
            cart.setSNum(1);
            shoppingsMapper.insert(cart);
            return RespBean.success("加入购物车成功");
        }else {
            addCartNum(ucart.getSId(),1);
            return RespBean.success("商品增加成功");
        }
    }

    /**
     * 清空购物车
     * @param userid
     * @return
     */
    @Override
    public RespBean delCartAll(Integer userid) {
        Integer count = shoppingsMapper.selectCount(new QueryWrapper<Shoppings>().eq("U_id", userid));
        if(count<1){
            return RespBean.error("购物车为空");
        }else {
            int u_id = shoppingsMapper.delete(new QueryWrapper<Shoppings>().eq("U_id", userid));
            return RespBean.success("清除成功");
        }
    }

    @Override
    public RespBean delCartOne(Integer userid, Integer cartid) {
        //判断是否是用户的购物车数据集
        Shoppings cart = shoppingsMapper.selectOne(new QueryWrapper<Shoppings>().eq("S_id", cartid));
        if (cart.getUId().equals(userid)){
            Shoppings ifCart = shoppingsMapper.selectOne(new QueryWrapper<Shoppings>().eq("S_id", cartid));
            if(ifCart.getSNum() <=1){
                delCart(userid,cartid);
                return RespBean.success("删除成功0",getCart(userid));
            }else {
                cart.setSNum(cart.getSNum()-1);
                shoppingsMapper.updateById(cart);
                return RespBean.success("删除成功1");
            }
        }
        return null;
    }

    /**
     * 勾选
     *
     * @param cartid
     * @param uId
     * @return
     */
    @Override
    public RespBean isSelected(Integer cartid, Integer uId) {
        Shoppings shopping = shoppingsMapper.selectOne(new QueryWrapper<Shoppings>()
                .eq("S_id", cartid));
        if (shopping.getUId().equals(uId)){

                shopping.setSState(1);
            int i = shoppingsMapper.updateById(shopping);
            if (i>0){
                return RespBean.success("勾选成功");
            }
        }
        return null;
    }

    /**
     * 不勾选
     *
     * @param cartid
     * @param uId
     * @return
     */
    @Override
    public RespBean noSelected(Integer cartid, Integer uId) {
        Shoppings shopping = shoppingsMapper.selectOne(new QueryWrapper<Shoppings>()
                .eq("S_id", cartid));
        if (shopping.getUId().equals(uId)){
            shopping.setSState(0);
            int i = shoppingsMapper.updateById(shopping);
            if (i>0){
                return RespBean.success("取消勾选成功");
            }
        }
        return null;
    }

    /**
     * 商品数量
     * @param userid
     * @param cartid
     * @param num
     * @return
     */
    @Override
    public RespBean ModifyCartNum(Integer userid, Integer cartid, Integer num) {
        int update = shoppingsMapper.update(null, new UpdateWrapper<Shoppings>()
                .eq("S_id", cartid)
                .eq("U_id", userid)
                .set("S_num", num)
        );
        if (update<=0){
            return RespBean.error("修改失败");
        }
        return RespBean.success("修改成功");
    }

    /**
     * 增加购物车数量
     * @param cartid
     * @param num
     * @return
     */
    @Override
    public RespBean addCartNum(Integer cartid, Integer num) {
        Shoppings shoppings = shoppingsMapper.selectById(cartid);
        shoppings.setSNum(shoppings.getSNum()+num);
        shoppingsMapper.updateById(shoppings);
        return RespBean.success("数量增加成功");
    }

    /**
     * 获取用户的购物车
     * @param userid
     * @return
     */
    @Override
    public RespBean getCart(Integer userid) {
        List<Shoppings> mycart = shoppingsMapper.selectList(new QueryWrapper<Shoppings>().eq("U_id", userid));
        List<CartVo> cartVos = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        for (Shoppings shopping : mycart) {

            Commodity commodity = commodityMapper.selectOne(new QueryWrapper<Commodity>().eq("C_id", shopping.getCId()));
            totalPrice = totalPrice.add(commodity.getCPrice().multiply(new BigDecimal(shopping.getSNum())));

            CartVo cartVo = new CartVo(shopping.getSId(),shopping.getCId(),commodity.getCName(),commodity.getCPrice(),commodity.getCImg(),
                    shopping.getSNum(),shopping.getSCreatetime());
            cartVos.add(cartVo);
        }
        CartResult cartResult = new CartResult(cartVos, totalPrice);
        return RespBean.success("查询成功",cartResult);
    }

    @Override
    public RespBean delCart(Integer userid, Integer cartid) {
        UpdateWrapper<Shoppings> shoppingsUpdateWrapper = new UpdateWrapper<>();
        shoppingsUpdateWrapper.eq("S_id", cartid).eq("U_id", userid);
        shoppingsMapper.delete(shoppingsUpdateWrapper);
        return RespBean.success("删除成功");
    }

    @Override
    public Integer cartTotal(Integer userid) {
        List<Shoppings> cart = shoppingsMapper.selectList(new QueryWrapper<Shoppings>().eq("U_id", userid));
        Integer cartTotal = 0;
        for (Shoppings shoppings : cart) {
            Integer sNum = shoppings.getSNum();
            cartTotal+=sNum;
        }
        return cartTotal;
    }
}
